package com.healthyeating.manage.controller;

import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.core.page.TableDataInfo;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.manage.domain.Share;
import com.healthyeating.manage.service.IMyShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 我的分享Controller
 *
 * @author 段奇乐
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/manage/share/myshare")
public class MyShareController extends BaseController {

    @Resource
    private IMyShareService myShareService;

    /**
     * 查询我的分享列表
     */
    @PreAuthorize("@ss.hasPermi('manage:share:list')")
    @GetMapping("/list")
    public TableDataInfo list(Share share) {
        startPage();
        List<Share> list = myShareService.selectShareList(share);
        return getDataTable(list);
    }

    /**
     * 导出我的分享列表
     */
    @PreAuthorize("@ss.hasPermi('manage:share:export')")
    @Log(title = "我的分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Share share) {
        List<Share> list = myShareService.selectShareList(share);
        ExcelUtil<Share> util = new ExcelUtil<Share>(Share.class);
        util.exportExcel(response, list, "我的分享数据");
    }

    /**
     * 获取我的分享详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:share:query')")
    @GetMapping(value = "/{shareId}")
    public AjaxResult getInfo(@PathVariable("shareId") Long shareId) {
        return success(myShareService.selectShareByShareId(shareId));
    }

    /**
     * 新增我的分享
     */
    @PreAuthorize("@ss.hasPermi('manage:share:add')")
    @Log(title = "我的分享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Share share) {
        return toAjax(myShareService.insertShare(share));
    }

    /**
     * 修改我的分享
     */
    @PreAuthorize("@ss.hasPermi('manage:share:edit')")
    @Log(title = "我的分享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Share share) {
        return toAjax(myShareService.updateShare(share));
    }

    /**
     * 删除我的分享
     */
    @PreAuthorize("@ss.hasPermi('manage:share:remove')")
    @Log(title = "我的分享", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shareIds}")
    public AjaxResult remove(@PathVariable Long[] shareIds) {
        return toAjax(myShareService.deleteShareByShareIds(shareIds));
    }
}
